package com.zx.www.volunteersystem.controller;

import com.zx.www.volunteersystem.mapper.Activity_attendanceMapper;
import com.zx.www.volunteersystem.pojo.Activity_attendance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/attendance")  // 统一使用/api前缀
@CrossOrigin(origins = "*")
public class Activity_attendanceContorller {

    @Autowired
    private Activity_attendanceMapper attendanceMapper;

    // 获取所有考勤记录 - 添加GET映射
    @GetMapping
    public List<Activity_attendance> getAllAttendances() {
        return attendanceMapper.selectAll();
    }

    // 根据ID查询考勤记录 - 添加GET映射
    @GetMapping("/{id}")
    public Activity_attendance getAttendanceById(@PathVariable Integer id) {
        return attendanceMapper.selectById(id);
    }

    // 根据活动ID查询所有参与记录 - 添加GET映射
    @GetMapping("/activity/{activityId}")
    public List<Activity_attendance> getAttendancesByActivityId(@PathVariable Integer activityId) {
        return attendanceMapper.selectByActivityId(activityId);
    }

    // 根据用户ID查询所有参与记录 - 添加GET映射
    @GetMapping("/user/{userId}")
    public List<Activity_attendance> getAttendancesByUserId(@PathVariable Integer userId) {
        return attendanceMapper.selectByUserId(userId);
    }

    // 添加考勤记录 - 添加POST映射
    @PostMapping
    public String addAttendance(@RequestBody Activity_attendance attendance) {
        try {
            int result = attendanceMapper.insert(attendance);
            return result > 0 ? "添加成功" : "添加失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败: " + e.getMessage();
        }
    }

    // 更新考勤记录 - 添加PUT映射
    @PutMapping("/{id}")
    public String updateAttendance(@PathVariable Integer id, @RequestBody Activity_attendance attendance) {
        try {
            attendance.setAttendance_id(id); // 设置ID
            int result = attendanceMapper.update(attendance);
            return result > 0 ? "更新成功" : "更新失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "更新失败: " + e.getMessage();
        }
    }

    // 删除考勤记录 - 添加DELETE映射
    @DeleteMapping("/{id}")
    public String deleteAttendance(@PathVariable Integer id) {
        try {
            int result = attendanceMapper.deleteById(id);
            return result > 0 ? "删除成功" : "删除失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败: " + e.getMessage();
        }
    }

    // 添加测试接口
    @GetMapping("/test")
    public String test() {
        return "考勤接口正常 - " + new java.util.Date();
    }
}